package gui.fileImportWindow;

import java.awt.Color;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.sql.SQLException;
import java.util.Observable;
import java.util.Observer;

import dataBase.DbHolder;

public class FileImportWindowController extends Thread implements Observer {
	FileImportWindow window;
	File file;
	CSVreader reader;
	ImportStatus status;
	boolean isRunning;

	public FileImportWindowController(File file, FileImportWindow window) {
		this.file = file;
		this.window = window;
		this.status = new ImportStatus();
		try {
			this.reader = new CSVreader(file);
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		this.status.addObserver(this);
	}

	public void setImportetDataNumber() {
		this.window.getSuccessfulImportsNumberLabel().setText(new Integer(this.status.getImported()).toString());
		this.window.getSuccessfulImportsNumberLabel().setForeground(Color.GREEN);
	}

	public void setAlreadyContainedDataNumber() {
		this.window.getAlreadyContainedDataNumberLabel().setText(new Integer(this.status.getExisting()).toString());
		this.window.getAlreadyContainedDataNumberLabel().setForeground(Color.orange);
	}

	public void setErronousDataNumber() {
		this.window.getErronousDataNumberLabel().setText(new Integer(this.status.getErronous()).toString());
		this.window.getErronousDataNumberLabel().setForeground(Color.RED);
	}

	private String getTextSoFar() {
		return this.window.getErrorText();
	}

	public void addAlreadyExistentComment(int lineNumber) {
		int displayedLineNumber = lineNumber + 1;
		String textToAdd = "Datensatz in Zeile " + displayedLineNumber + " ist bereits in der Datenbank\n";
		this.window.setErrorText(this.getTextSoFar() + textToAdd);
	}

	public void addWronLengthComment(int lineNumber) {
		int displayedLineNumber = lineNumber + 1;
		String textToAdd = "Falsche Spaltenanzahl in Zeile " + displayedLineNumber + "\n";
		this.window.setErrorText(this.getTextSoFar() + textToAdd);
	}

	public void addErrorInLineComment(int lineNumber, int column) {
		int displayedLineNumber = lineNumber + 1;
		int displayedColumnNumber = column + 1;
		String textToAdd = "Fehlerhafter Datensatz in Zeile " + displayedLineNumber + ", Spalte" + displayedColumnNumber + "\n";
		this.window.setErrorText(this.getTextSoFar() + textToAdd);
	}

	@Override
	public void run() {
		this.isRunning = true;
		int lines = 0;
		try {
			lines = this.reader.getNumberOfLines();
		} catch (IOException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}

		this.window.setProgressBar(lines);

		int i = 0;

		while ((i < lines) && this.isRunning) {

			/** if line has no valid length */
			try {
				if (!this.reader.hasValidLength(i)) {
					this.addWronLengthComment(i);
					this.status.setErronous(this.status.getErronous() + 1);
				} else {
					/** if length is valid - check for errors in line */
					boolean [] tmp = this.reader.isValid(i);
					boolean totallyValid = true;

					for (int j = 0; j < tmp.length; j++) {
						if (!tmp [j]) {
							totallyValid = false;
							this.addErrorInLineComment(i, j);
							this.status.setErronous(this.status.getErronous() + 1);
						}
					}
					/** if no errors are found */
					if (totallyValid) {
						/** player already in database */
						if (this.reader.isAlreadyInDB(i)) {
							this.status.setExisting(this.status.getExisting() + 1);
							this.addAlreadyExistentComment(i);
						} else {
							/** add player */
							try {
								this.reader.saveLineToDB(i);
								this.status.setImported(i);
							} catch (SQLException e) {
								// TODO Auto-generated catch block
								e.printStackTrace();
							}
						}
					}
				}
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}

			i++;
		}
		DbHolder.getInstance().getArcherData().notifyObservers();
		this.window.getCloseButton().setText("Schließen");
	}

	@Override
	public void update(Observable arg0, Object arg1) {
		this.setImportetDataNumber();
		this.setAlreadyContainedDataNumber();
		this.setErronousDataNumber();
		this.window.incrementProgressBar();

	}

	public void closeAction() {
		this.isRunning = false;
		this.window.dispose();
	}
}
